package xyz.xtt.schedule.service;

import java.math.BigInteger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tongtong.middleware.vo.params.bsn.RechargeRequest;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class CheckAndRechargeService {
	// 4200000000000000 = 1元; 840000000000000000 = 200元
	public final BigInteger minEnergyValue = new BigInteger("840000000000000000");
	public final float minComputingPowerValue = 500f;
	public final int perRechargeValue = 200;
	public final String account = "0x3b5Be4c33249458534fcaa38Aa38B194E1d0EFC4";

	@Autowired
	private InnerService innerService;

	public void executeTask() {
		// 先查询算力值和能量值
		BigInteger currEnergyValue = innerService.getBalance(account);
		Double currComputingPowerValue = innerService.balance();
		log.info("executeTask: currEnergyValue=>{} currComputingPowerValue=>{}", currEnergyValue, currComputingPowerValue);
		// 跟最小值对比，如果小于最小值则触发提醒或者充值
		if (minComputingPowerValue > currComputingPowerValue) {
			log.error("executeTask: 余额不足 currComputingPowerValue=>{} minComputingPowerValue=>{}", currComputingPowerValue, minComputingPowerValue);
		}
		if (minEnergyValue.compareTo(currEnergyValue) > 0) {
			RechargeRequest rechargeRequest = new RechargeRequest();
			rechargeRequest.setOpbChainId(4);
			rechargeRequest.setOpbChainClientAddress(account);
			rechargeRequest.setHashRateValue(perRechargeValue);
			String res = innerService.recharge(rechargeRequest);
			log.error("executeTask: 余额不足 currEnergyValue=>{} minEnergyValue=>{} 充值金额=>{}", currEnergyValue, minEnergyValue, res);
		}
	}
}
